﻿// extern alias MyAlias;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Runtime;
using Android.Support.V7.App;
using Android.Util;
using Android.Views;
using Android.Views.InputMethods;
using Android.Widget;
using API;
using mb.DroidLib;

namespace mb
{
    public partial class App
    {
        public static Color MainColor = Color.Rgb(251, 114, 153);
        public static Color MainBKColor = Color.Rgb(244, 244, 244);
    }
}

namespace mb.Droid
{
    [Activity(Label = "视频阅读器", MainLauncher = true,
        Icon = "@drawable/xlogo",
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
        ScreenOrientation = ScreenOrientation.Portrait
    )]
    public class StartActivity : AppCompatActivity
    {
        public static StartActivity This;
        public int ContentViewId;

        public Action<Configuration> MyOnConfiguurationChanged;
        public Func<MotionEvent, bool> MyDispatchTouchEvent;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState);

            RequestWindowFeature(WindowFeatures.NoTitle);

            This = this;
            App.Initialize();
            ServiceLocator.Instance.Get<IStartOption>().LoadStartOption();

            Xamarin.Forms.Forms.Init(this, savedInstanceState);
            FFImageLoading.Forms.Platform.CachedImageRenderer.Init(enableFastRenderer: true);
            var ike = Plugin.InputKit.CrossInputKit.IsSupported;

            LoadFragment(typeof(StartFragment));
        }

        public override void OnConfigurationChanged(Configuration newConfig)
        {
            base.OnConfigurationChanged(newConfig);
            MyOnConfiguurationChanged?.Invoke(newConfig);
        }

        bool isDispatchingMy;
        public override bool DispatchTouchEvent(MotionEvent e)
        {
            if (isDispatchingMy || MyDispatchTouchEvent == null)
                return base.DispatchTouchEvent(e);

            isDispatchingMy = true;
            bool ret = MyDispatchTouchEvent(e);
            isDispatchingMy = false;
            return ret;

        }

        public void LoadFragment(Type type)
        {
            LoadFragment(Activator.CreateInstance(type) as Android.Support.V4.App.Fragment);
        }
        public void LoadFragment(Android.Support.V4.App.Fragment frag)
        {
            var view = new FrameLayout(this) { Id = View.GenerateViewId() };
            SetContentView(view);
            ContentViewId = view.Id;
            SupportFragmentManager.BeginTransaction()
                .Add(view.Id, frag)
                .Commit();
        }

    }
}